package me.hys.api.user;

import java.util.List;

import me.hys.api.BaseApi;
import me.hys.base.ApiResult;
import me.hys.base.PageQuery;
import me.hys.chat.comm.ChatConstants;
import me.hys.entity.doctor.AnswerEntity;
import me.hys.service.chat.IChatManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author: @author HYS . <br>
 * Date: 2015-10-6 上午1:40:41 . <br>
 * Function: 消息相关
 */
@Controller
@RequestMapping("/api/chat")
public class ChatApi extends BaseApi {
	@Autowired
	private IChatManager chatManager;

	/**
	 * @Title : delete
	 * @Description : 删除用户
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ApiResult delete(@RequestParam String userid) {
		return chatManager.delete(ChatConstants.NAME_PREFIX + userid);
	}

	/**
	 * @Title : getMsg
	 * @Description : 获取消息记录
	 */
	@ResponseBody
	@RequestMapping(value = "/list/{diagnoseId}/{start}", method = RequestMethod.GET)
	public List<AnswerEntity> getMsg(@PathVariable String diagnoseId,
			@PathVariable int start) {
		PageQuery<AnswerEntity> page = getPageQuery(start);
		page.getCondition().put("diagnoseId", diagnoseId);
		PageQuery<AnswerEntity> result = chatManager.findMsgPage(page);
		return result.getList();
	}

	/**
	 * @Title : login
	 * @Description : 用户登录
	 */
	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ApiResult login(@RequestParam String userid,
			@RequestParam String mobile) {
		return chatManager.login(userid, mobile);
	}

	/**
	 * @Title : modifyPassword
	 * @Description : 修改密码
	 */
	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public ApiResult modify(@RequestParam String userid,
			@RequestParam String mobile) {
		return chatManager.modifyPassword(userid, mobile);
	}

	/**
	 * @Title : register
	 * @Description : 注册用户
	 */
	@ResponseBody
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ApiResult register(@RequestParam String userid,
			@RequestParam String mobile, @RequestParam String nickname) {
		return chatManager.register(userid, mobile, nickname);
	}

	/**
	 * @Title : sendMsg
	 * @Description : 给用户发送一条消息
	 */
	@ResponseBody
	@RequestMapping(value = "/post", method = RequestMethod.POST)
	public ApiResult sendMsg(@RequestParam String msg,
			@RequestParam String from, @RequestParam String target) {
		return chatManager.sendMsg(msg, from, target);
	}
}